package com.lv.service.room.goods

import android.content.Context
import android.os.Parcelable
import androidx.annotation.Keep
import androidx.lifecycle.LiveData
import androidx.room.Dao
import androidx.room.Database
import androidx.room.Delete
import androidx.room.Entity
import androidx.room.Insert
import androidx.room.OnConflictStrategy
import androidx.room.PrimaryKey
import androidx.room.Query
import androidx.room.Room
import androidx.room.RoomDatabase
import androidx.room.TypeConverters
import androidx.room.Update
import com.lv.service.bean.AttributeBean
import com.lv.service.bean.ContractBean
import com.lv.service.bean.DescribesBean
import com.lv.service.bean.Sku
import com.lv.service.bean.SpecitemsBean
import com.lv.service.room.helper.InspectionContractConverter
import com.lv.service.room.helper.InspectionGoodAttributeConverter
import com.lv.service.room.helper.InspectionGoodDescribesConverter
import com.lv.service.room.helper.InspectionGoodsSkuConverter
import com.lv.service.room.helper.InspectionSpecitemsBeanConverter
import com.lv.service.room.helper.InspectionStringConverter

import kotlinx.parcelize.Parcelize

/**
 * 作者： ygx
 * 创建日期：2023/3/23 9:29 AM
 * 签名： 天行健，君子以自强不息；地势坤，君子以厚德载物。
 * -    _              _           _     _   ____  _             _ _
 * -   / \   _ __   __| |_ __ ___ (_) __| | / ___|| |_ _   _  __| (_) ___
 * -  / _ \ | '_ \ / _` | '__/ _ \| |/ _` | \___ \| __| | | |/ _` | |/ _ \
 * - / ___ \| | | | (_| | | | (_) | | (_| |  ___) | |_| |_| | (_| | | (_) |
 * -/_/   \_\_| |_|\__,_|_|  \___/|_|\__,_| |____/ \__|\__,_|\__,_|_|\___/
 * <p>
 * You never know what you can do until you try !
 * ----------------------------------------------------------------
 * 描述：购物车的数据库
 */

//3、database
@Database(entities = [GoodsDetailsBeans::class], version = 1, exportSchema = false)
abstract class GoodsCatDatabase : RoomDatabase() {

    abstract val goodsDao: GoodsDao

    companion object {

        private const val USER_DB_NAME = "syy_goods_db"

        @Volatile
        private var instance: GoodsCatDatabase? = null

        @Synchronized
        fun getInstance(context: Context): GoodsCatDatabase {
            return instance ?: Room.databaseBuilder(
                context,
                GoodsCatDatabase::class.java,
                USER_DB_NAME
            ).build().also { instance = it }
        }
    }
}

//1、entity的定义
@TypeConverters(
    InspectionGoodsSkuConverter::class,
    InspectionGoodDescribesConverter::class,
    InspectionGoodAttributeConverter::class,
    InspectionContractConverter::class,
    InspectionStringConverter::class,
    InspectionSpecitemsBeanConverter::class
)
@Entity(tableName = "goods_db")
@Parcelize
@Keep
data class GoodsDetailsBeans(
    @PrimaryKey
    val _id: Int,//主键
    val id: String? = null,
    val name: String? = null,
    val cpyid: String? = null,
    val wantnum: Int? = 0,
    val salenum: Int? = 0,
    val islisted: String? = null,
    val isaudit: String? = null,
    val image: String? = null,
    val images: List<String>?,
    val fcaid: Int? = 0,
    val fcname: String? = null,
    val secaid: Int? = null,
    val secname: String? = null,
    val thcaid: Int? = null,
    val thcname: String? = null,
    val introduction: String? = null,
    val totalname: String? = null,
    val price: Double?,
    val reducedprice: Double?,
    val collectionnum: Int? = 0,
    val skusize: Int? = 0,
    val province: String? = null,
    val city: String? = null,
    val batch: Int? = 0,
    val status: String? = null,
    val attribute: List<AttributeBean>? = null,
    val cpname: String? = null,
    val cplogo: String? = null,
    val isspecially: String? = null,
    val benefit: String? = null,
    val promiseimage: String? = null,
    val promise: String? = null,
    val skuList: List<Sku>? = null,
    val coordinates: List<DescribesBean>?,
    val attentionnum: Int? = null,
    val specitems: List<SpecitemsBean>?,
    val contract: List<ContractBean>?,
    val address: String? = null,
    val area: String? = null,
    val backdrop: String? = null,
    val bname: String? = null,
    val created: Long? = 0,
    val updated: Long? = 0,
    val display: String? = null,
    val issale: String? = null,
    val reseq: Long,
    val seq: String? = null,
    val txaccid: String? = null,
) : Parcelable


////1、entity的定义
//
//@Entity(tableName = "tb_cniao_user")
//data class CniaoUserInfo(
//    @PrimaryKey
//    val id: Int,//主键
//    val course_permission: Boolean,
//    val token: String?,
//    @Embedded
//    val user: User?
//) {
//    @Keep
//    data class User(
//        @ColumnInfo(name = "cniao_user_id")
//        val id: Int,//用户id
//        val logo_url: String?,//用户头像
//        val reg_time: String?,//用户注册时间
//        val username: String?//用户名
//    )
//}

//2、dao层的定义
@Dao
interface GoodsDao {

    @Insert(onConflict = OnConflictStrategy.REPLACE)
    fun insertFindGoods(GoodsDetailsBeans: GoodsDetailsBeans)

    @Insert(onConflict = OnConflictStrategy.REPLACE)
    suspend fun insertFindSuspendGoods(GoodsDetailsBeans: GoodsDetailsBeans)

    @Insert(onConflict = OnConflictStrategy.REPLACE)
    suspend fun insertFindSuspendGoods(GoodsDetailsBeans: List<GoodsDetailsBeans>)

    @Update(onConflict = OnConflictStrategy.REPLACE)
    fun updateFindGoods(categoryBean: GoodsDetailsBeans)

    @Delete
    fun deleteFindGoods(categoryBean: GoodsDetailsBeans)

    @Query("delete from goods_db")
    fun deleteAllGoods()

    @Query("delete from goods_db")
    suspend fun deleteSuspendAllGoods()

    // = , in , like
    @Query("select * from goods_db where _id=:id")
    fun queryLiveFindGoods(id: Int = 0): LiveData<GoodsDetailsBeans>

    @Query("select * from goods_db")
    fun queryLiveFindAllGoods(): LiveData<List<GoodsDetailsBeans>>

//    @Query("select * from category_db where _id=:id")
//    fun queryFindCategory(id: Int = 0): CategoryBean?

//    @Query("select * from category_db")
//    fun queryFindAllCategory(): List<CategoryBean>?

    @Query("select * from goods_db where _id =:id")
    suspend fun getFindGoods(id: Int = 0): GoodsDetailsBeans?

    @Query("select * from goods_db")
    suspend fun getAllGoods(): List<GoodsDetailsBeans>?
}



